package com.ourteam.product.requirement.dao.impl;

import com.ourteam.product.requirement.dao.IProductCatalogDAO;
import com.ourteam.product.requirement.dao.ProductCatalog;
import com.ourteam.product.requirement.dao.ProductCatalogDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 CST 2017
 * Auto Generate Date : 2018-05-03 16:45:36
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractProductCatalogDAOJdbcImpl extends AbstractSpringDAO
    implements IProductCatalogDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            CatalogName, Id, ParentId, Remarks, Status, SortIndex
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.CatalogName, _DataObjectProperty.Id,
            _DataObjectProperty.ParentId, _DataObjectProperty.Remarks,
            _DataObjectProperty.Status, _DataObjectProperty.SortIndex
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<ProductCatalog, ProductCatalogDAOQueryBean> daoHelper;

    public AbstractProductCatalogDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<ProductCatalog, ProductCatalogDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aProductCatalog
         * @throws Exception
         */
    public boolean checkIsUnique(ProductCatalog aProductCatalog)
        throws Exception {
        if (aProductCatalog == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aProductCatalog.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aProductCatalog
     * @throws Exception
     */
    public void insert(ProductCatalog aProductCatalog)
        throws Exception {
        super.doInsert(aProductCatalog);
    }

    /**
         * Batch Insert Datas
         * @param aProductCatalog
         * @throws Exception
         */
    public void batchInsert(ProductCatalog[] aProductCatalogs)
        throws Exception {
        super.doBatchInsert(aProductCatalogs);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(ProductCatalogDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(ProductCatalog aProductCatalog)
        throws Exception {
        delete(aProductCatalog.getId());
    }

    /**
     * Update Data Selective
     * @param aProductCatalog
     * @throws Exception
     */
    public void update(ProductCatalog aProductCatalog)
        throws Exception {
        if (aProductCatalog.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aProductCatalog);

        ProductCatalog tempObj = findById(aProductCatalog.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aProductCatalog, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aProductCatalogs[]
     * @throws Exception
     */
    public void batchUpdate(ProductCatalog[] aProductCatalogs)
        throws Exception {
        super.doBatchUpdate(aProductCatalogs);
    }

    /**
         * Update Data By Condictions
         * @param aProductCatalog
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(ProductCatalog aProductCatalog,
        ProductCatalogDAOQueryBean aQueryBean) throws Exception {
        super.doUpdateByQuery(aProductCatalog, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(ProductCatalogDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(ProductCatalog aProductCatalog)
        throws Exception {
        if (aProductCatalog.getRemarks() == null) {
            throw new Exception(
                "The ProductCatalog.remarks value is not allow NULL");
        }

        if (aProductCatalog.getStatus() == null) {
            throw new Exception(
                "The ProductCatalog.status value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public ProductCatalog findById(final long id) throws Exception {
        ProductCatalogDAOQueryBean queryBean = new ProductCatalogDAOQueryBean();

        ProductCatalogDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        ProductCatalog[] ProductCatalogs = query(queryBean);

        if (ProductCatalogs.length == 0) {
            return null;
        } else {
            return ProductCatalogs[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return ProductCatalog[]
         * @throws Exception
         */
    public ProductCatalog[] query(ProductCatalogDAOQueryBean queryBean)
        throws Exception {
        List result = super.doQuery(queryBean, new JdbcProductCatalogRowMapper());

        return (ProductCatalog[]) result.toArray(new ProductCatalog[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        ProductCatalogDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(ProductCatalogDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(ProductCatalog[] aProductCatalogs,
        ProductCatalogDAOQueryBean queryBean) throws Exception {
        daoHelper.synchronize(aProductCatalogs, queryBean);
    }

    public void synchronize(ProductCatalog[] aProductCatalogs,
        ProductCatalogDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<ProductCatalog> callBack)
        throws Exception {
        daoHelper.synchronize(aProductCatalogs, queryBean, callBack);
    }

    public static class JdbcProductCatalogRowMapper implements RowMapper<ProductCatalog> {
        public ProductCatalog mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            ProductCatalog object = new ProductCatalog();

            int columnIndex = 1;

            int __dataType;

            try {
                Object catalogName = null;

                catalogName = resultSet.getObject(columnIndex++);

                object.setCatalogNameNull(resultSet.getBoolean(columnIndex++));

                if (catalogName != null) {
                    setBeanValue(object, _DataObjectProperty.CatalogName,
                        catalogName);
                }

                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object parentId = null;

                parentId = resultSet.getObject(columnIndex++);

                object.setParentIdNull(resultSet.getBoolean(columnIndex++));

                if (parentId != null) {
                    setBeanValue(object, _DataObjectProperty.ParentId, parentId);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object sortIndex = null;

                sortIndex = resultSet.getObject(columnIndex++);

                object.setSortIndexNull(resultSet.getBoolean(columnIndex++));

                if (sortIndex != null) {
                    setBeanValue(object, _DataObjectProperty.SortIndex,
                        sortIndex);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
